﻿<script lang="ts" name="planSharp" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { usePlanSharpApi } from '/@/api/plans/planSharp';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const planSharpApi = usePlanSharpApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  ch: [{required: true, message: '请选择船号！', trigger: 'blur',},],
  startSharpTime: [{required: true, message: '请选择开工！', trigger: 'change',},],
  offDockTime: [{required: true, message: '请选择下坞！', trigger: 'change',},],
  floatTime: [{required: true, message: '请选择起浮！', trigger: 'change',},],
  fdTotal: [{required: true, message: '请选择分段量！', trigger: 'blur',},],
  swsInt: [{required: true, message: '请选择本部！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await planSharpApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await planSharpApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="planSharp-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="船坞" prop="dock">
								<el-input v-model="state.ruleForm.dock" placeholder="请输入船坞" maxlength="32" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="批次" prop="batch">
								<el-input v-model="state.ruleForm.batch" placeholder="请输入批次" maxlength="64" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="船级社" prop="classificationSociety">
								<el-input v-model="state.ruleForm.classificationSociety" placeholder="请输入船级社" maxlength="64" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="船型" prop="shipType">
								<el-input v-model="state.ruleForm.shipType" placeholder="请输入船型" maxlength="64" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="船东" prop="shipOwner">
								<el-input v-model="state.ruleForm.shipOwner" placeholder="请输入船东" maxlength="64" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="坞前" prop="dockBefore">
								<el-input v-model="state.ruleForm.dockBefore" placeholder="请输入坞前" maxlength="64" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="坞后" prop="dockAfter">
								<el-input v-model="state.ruleForm.dockAfter" placeholder="请输入坞后" maxlength="64" show-word-limit clearable />
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
							<el-form-item label="船号" prop="ch">
								<el-input v-model="state.ruleForm.ch" placeholder="请输入船号" maxlength="32" show-word-limit clearable />
							</el-form-item>
						</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="开工" prop="startSharpTime">
							<el-date-picker v-model="state.ruleForm.startSharpTime" type="date" placeholder="开工" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="下坞" prop="offDockTime">
							<el-date-picker v-model="state.ruleForm.offDockTime" type="date" placeholder="下坞" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="起浮" prop="floatTime">
							<el-date-picker v-model="state.ruleForm.floatTime" type="date" placeholder="起浮" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="分段量" prop="fdTotal">
							<el-input-number v-model="state.ruleForm.fdTotal" placeholder="请输入分段量" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="本部" prop="swsInt">
							<el-input-number v-model="state.ruleForm.swsInt" placeholder="请输入本部" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="外协" prop="otherInt">
							<el-input-number v-model="state.ruleForm.otherInt" placeholder="请输入外协" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="最早计划" prop="firstPStrTime">
							<el-date-picker v-model="state.ruleForm.firstPStrTime" type="date" placeholder="最早计划" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="最早开工" prop="firstStrTime">
							<el-date-picker v-model="state.ruleForm.firstStrTime" type="date" placeholder="最早开工" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="最后结束" prop="lastEndTime">
							<el-date-picker v-model="state.ruleForm.lastEndTime" type="date" placeholder="最后结束" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="进度" prop="jindu">
							<el-input-number v-model="state.ruleForm.jindu" placeholder="请输入进度" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="状态" prop="statusAll">
							<g-sys-dict v-model="state.ruleForm.statusAll" code="FinishStatusEnum" render-as="select" placeholder="请选择状态" clearable filterable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>
